0%

[NOI 2005] 智慧珠游戏 题解

大家的都太长了,发一个比较短的代码,洛谷上测的时间为800ms。思路大概是,暴力枚举,还有一些剪枝。用了一些奇技淫巧,没有删除调试代码,也没有压行。可以注意一下预处理的方式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <bits/stdc++.h>
using namespace std;
const int N = 11;
const int n = 10;
const char *mmm[] =
{
"AA",
"A ",
"",
"BBBB",
"",
"CCC",
"C ",
"",
"DD",
"DD",
"",
"E ",
"E ",
"EEE",
"",
"FFFF",
" F ",
"",
"GGG",
"G G",
"",
"HHH",
"HH ",
"",
"III ",
" II",
"",
" J ",
"JJJ",
" J ",
"",
"K ",
"KK ",
" KK",
"",
"LLLL",
"L ",
"",
0
};

struct node
{
char state[5][5];
int fx;
int height;
int width;
node():fx(9999), height(0), width(0) {memset(state, 0, sizeof state);}
int fix()
{
if(fx != 9999) return fx;
for(int i = 0; i < width; ++i)
{
if(isalpha(state[0][i]))
return fx = i;
}
return -9999;
}
char *operator[](size_t i)
{
return state[i];
}
const char *operator[](size_t i) const
{
return state[i];
}
int ID()
{
return state[0][fix()];
}
node rotate() const
{
node nd;
nd.height = width;
nd.width = height;
for(int i = 0; i < width; ++i)
{
for(int j = 0; j < height; ++j)
nd[i][j] = state[j][width - 1 - i];
}

return nd;
}
node reverse() const
{
node nd;
nd.height = height;
nd.width = width;
for(int i = 0; i < height; ++i)
memcpy(nd[i], state[height - 1 -i], width + 1);
return nd;
}
void show()
{
for(int i = 0; i < height; ++i)
{
for (int j = 0; j < width; j++) {
cerr << state[i][j];
}
cerr << endl;
}
cerr << endl;
}

};
bool operator<(node &lhs, node &rhs)
{
if(lhs.ID() != rhs.ID()) return lhs.ID() < rhs.ID();
if(lhs.height != rhs.height) return lhs.height < rhs.height;
if(lhs.width != rhs.width) return lhs.width < rhs.width;
for(int i = 0; i < lhs.height; ++i)
if(strcmp(lhs[i], rhs[i]) != 0)
return strcmp(lhs[i], rhs[i]) < 0;
return false;
}
bool operator==(node &lhs, node &rhs)
{
return !(lhs < rhs) && !(rhs < lhs);
}
node mmp[12 * 8];
int len;
void init()
{
const char **p = mmm;
while(*p)
{
node nd;
while(*p && **p)
{
strcpy(nd[nd.height++], *p);
++p;
}
nd.width = strlen(nd[0]);
mmp[len++] = (nd);
mmp[len++] = (nd.rotate());
mmp[len++] = (nd.rotate().rotate());
mmp[len++] = (nd.rotate().rotate().rotate());
mmp[len++] = (nd.reverse());
mmp[len++] = (nd.rotate().reverse());
mmp[len++] = (nd.rotate().rotate().reverse());
mmp[len++] = (nd.rotate().rotate().rotate().reverse());
++p;
}
sort(mmp, mmp + len);
len = unique(mmp, mmp + len) - mmp;
random_shuffle(mmp, mmp + len);
}
int used[255];
char mp[N][N];
void show_map()
{
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= i; ++j)
cout << mp[i][j];
cout << endl;
}
cout << endl;
}
bool check(int x, int y, int id)
{
const node &nd = mmp[id];
if(x + nd.height - 1 > n) return false;
if(y + nd.width - 1 > n) return false;
for(int i = 0; i < nd.height; ++i)
{
for(int j = 0; j < nd.width; ++j)
{
if(isalpha(nd[i][j]) && mp[x+i][y+j] != '.')
return false;
}
}
return true;
}
bool put(int x, int y, int id, bool unput)
{
const node &nd = mmp[id];
for(int i = 0; i < nd.height; ++i)
{
for(int j = 0; j < nd.width; ++j)
{
if(isalpha(nd[i][j]))
{
if(unput)
mp[x + i][y + j] = '.';
else
mp[x + i][y + j] = nd[i][j];
}
}
}
return true;
}

int pre[N*N];
int find(int i)
{
if(pre[i] > 0)
return pre[i] = find(pre[i]);
else return i;
}
void merge(int a, int b)
{
int ra = find(a), rb = find(b);
if(ra == rb) return;
pre[ra] -= -pre[rb] + 1;
pre[rb] = ra;
}
bool checkcn()
{
memset(pre, 0, sizeof pre);
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= i; ++j)
{
if(mp[i][j] == '.')
{
if(mp[i][j-1] == '.')
merge(i*n + j, i * n + j - 1);
if(mp[i-1][j] == '.')
merge(i*n + j, (i - 1) * n + j);
}
}
}
// for(int i = 1; i <= n; ++i)
// {
// for(int j = 1; j <= i; ++j)
// {
// cerr << -pre[find(i * n + j)] + 1 << " ";
// }
// cerr << endl;
// }
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= i; ++j)
{
if(mp[i][j] == '.' && -pre[find(i * n + j)] + 1 < 3)
return false;
}
}
return true;
}
clock_t b ;
void dfs(int x, int y)
{
if(!checkcn())
{
return;
}
while(x <= n && isalpha(mp[x][y]))
{
++y;
if(y > x)
y = 1, x += 1;
}
if(x == n + 1)
{
show_map();
cerr << 1.0 * (clock() - b) / CLOCKS_PER_SEC << endl;
exit(0);
}
// show_map();
for(int i = 0; i < len; ++i)
{
int ny = y - mmp[i].fix();
if(!used[mmp[i].ID()] && check(x, ny, i))
{
put(x, ny, i, false);
used[mmp[i].ID()] = 1;
dfs(x, y + 1);
used[mmp[i].ID()] = 0;
put(x, ny, i, true);
}
}
}


#define IO(file) freopen(#file".in", "r", stdin), freopen(#file".out", "w", stdout)
int main()
{
IO(game);
// freopen("game.out", "w", stdout);
memset(mp, 'X', sizeof mp);
init();
// for(int i = 0; i < len; ++i) {
// cout << mmp[i].ID() << endl;
// mmp[i].show();
// }

for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= i; ++j)
{
cin >> mp[i][j];
if(isalpha(mp[i][j]))
{
used[mp[i][j]] = 1;
}
}
}

b = clock();
dfs(1, 1);
cout << "No solution" << endl;
}